import { defineStore } from "pinia"
import { ref } from "vue"

export const useMessageSpaceStore = defineStore("message_spcae", () => {
    const chatterKey = ref(new Date() + '')
    const chatterList = ref<any>([]);
    const addChatter = (chatterMsg: any) => {
        let newChatterList = chatterList.value.filter(
            (item: any) => item.id != chatterMsg.id
        );
        chatterList.value = [chatterMsg, ...newChatterList]
        chatterKey.value = new Date() + ''
    }
    const getNewChatterList = (newChatterList: any) => {
        chatterList.value = [...newChatterList]
        chatterKey.value = new Date() + '1'
    }

    const historyRecordList = ref<any>([])
    const addHistoryRecord = (msg: any) => {
        historyRecordList.value = [...historyRecordList.value, msg]
    }
    const setHistoryRecordList = (list: Array<any>) => {
        historyRecordList.value = list
    }
    return {
        chatterList,
        addChatter,
        getNewChatterList,
        chatterKey,
        historyRecordList,
        addHistoryRecord,
        setHistoryRecordList
    }
}, {
    persist: {
        storage: sessionStorage
    }
})